﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using System.Collections.ObjectModel;
using ARR.UI.Models;
using ARR.Shared;
using System.Windows.Input;
using ARR.UI.Commands;
using System.Windows;
using System.Collections;
using DDF.Entity;
using System.ComponentModel;
using telerik = Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;


namespace ARR.UI.ViewModels
{
    class StoreMasterViewModel : WorkspaceViewModel
    {
        private IStoreMasterModel model;
        private ICommand deleteCommand;
        private ICommand saveCommand;
        private ICommand selectionChangedCommand;
        private ICommand rowValidatingCommand;


        public StoreMasterViewModel(IStoreMasterModel model)
        {
            this.model = model;
            this.DisplayName = Labels.MaestroDeTiendas;

            SelectedStores = new List<Store>();
        }



        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand(param => DeleteStores(), param => SelectedStores != null);
                }
                return deleteCommand;
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(
                        param => SaveStore());
                }
                return saveCommand;
            }
        }

        public ICommand SelectionChangedCommand
        {
            get
            {
                if (selectionChangedCommand == null)
                {
                    selectionChangedCommand = new RelayCommand(e => GetSelectedItems(e as telerik.SelectionChangeEventArgs));
                }
                return selectionChangedCommand;
            }
        }

        public ICommand RowValidatingCommand
        {
            get
            {
                if (rowValidatingCommand == null)
                {
                    rowValidatingCommand = new RelayCommand(e => OnRowValidating(e as telerik.GridViewRowValidatingEventArgs));
                }
                return rowValidatingCommand;
            }
        }
                                
        public ObservableCollection<Store> Stores
        {
            get
            {
                return model.Stores;
            }
        }

        public List<Store> SelectedStores { get; set; }

        public ObservableCollection<Country> Countries
        {
            get
            {
                return model.Countries;
            }
        }


        
        private void OnRowValidating(telerik.GridViewRowValidatingEventArgs e)
        {
           Store store = e.Row.DataContext as Store;

            if (e.EditOperationType == GridViewEditOperationType.Insert)
            {
                if (!store.IsValid)
                {
                    e.IsValid = false;
                }
            }
        }

        private void GetSelectedItems(telerik.SelectionChangeEventArgs e)
        {
            List<Store> added = e.AddedItems.Cast<Store>().ToList();
            List<Store> removed = e.RemovedItems.Cast<Store>().ToList();

            SelectedStores.AddRange(added);
            removed.ForEach(st => SelectedStores.Remove(st));
        }

        private void DeleteStores()
        {
            model.DeleteStore(SelectedStores);
            OnPropertyChanged<StoreMasterViewModel>(vm => vm.Stores);
        }

        private void SaveStore()
        {
            if (AreValid())
            {
                model.Save();

                OnPropertyChanged<StoreMasterViewModel>(vm => vm.Stores);
            }            
        }

        private bool AreValid()
        {
            foreach (Store st in Stores)
            {
                if (!st.IsValid)
                    return false;
            }

            return true;
        }

    }
}
